package com.xplusplus.security.vo;

import com.xplusplus.security.domain.*;
import com.xplusplus.security.repository.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author: zhouweixin
 * @Description: 导入用户类
 * @Date: Created in 16:06 2018/10/24
 * @Modified By:
 */
@Service
public class UserExcel {
    private int rowNum = 0;
    // 部门: 非空
    private Department department;
    // 姓名: 非空
    private String name;
    // 联系电话: 非空
    private String contact;
    // 性别:0男; 1女
    private int sex;
    // 出生日期
    private Date bornDate;
    // 开始有效期：yyyy-MM-dd
    private Date startDate;
    // 截止有效期：yyyy-MM-dd
    private Date endDate;

    // 银行卡号
    private String cardNum;
    // 身份证号
    private String identityNum;
    // 民族
    private Nation nation;
    // 地址
    private String address;
    // 签发机关
    private String qianfajiguan;
    // 项目名称
    private Project project;
    // 介绍人
    private String sponsorName;
    // 文件程度
    private Education education;
    // 身高
    private double height = 0.0;
    // 政治面貌
    private PoliticalStatus politicalStatus;
    // 兵役
    private MilitaryStatus militaryStatus;
    // 婚姻状况
    private MaritalStatus maritalStatus;
    // 保险情况
    private int insurance;
    // 缴纳单位
    private String jiaonadanwei;
    // 工资
    private Date employDate;
    // 入职时间
    private double baseWage = 0;
    private Role role;
    private JobNature jobNature;

    private final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");


    @Autowired
    private NationRepository nationRepository;

    @Autowired
    private DepartmentRepository departmentRepository;

    @Autowired
    private ProjectRepository projectRepository;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private EducationRepository educationRepository;

    @Autowired
    private PoliticalStatusRepository politicalStatusRepository;

    @Autowired
    private MilitaryStatusRepository militaryStatusRepository;

    @Autowired
    private MaritalStatusRepository maritalStatusRepository;

    public UserExcel() {
    }

    public Role getRole() {
        return role;
    }

    public void setRole(Role role) {
        this.role = role;
    }

    public UserExcel(int rowNum) {
        this.rowNum = rowNum;
    }

    /**
     * 解析字段
     *
     * @param key
     * @param value
     */
    public void parse(UserExcel userExcel, String key, String value) {
        switch (key){
            case "部门":
                Department department = departmentRepository.findFirstByNameLike(String.format("%%%s%%", value));
                if(department != null){
                    userExcel.department = department;
                }
                break;
            case "姓名":
                userExcel.name = value;
                break;
            case "性别":
                if("0".equals(value) || "男".equals(value) || "male".equals(value)){
                    userExcel.sex = 0;
                } else {
                    userExcel.sex = 1;
                }
                break;
            case "联系电话":
                userExcel.contact = value;
                break;
            case "出生日期":
                try {
                    value = value.replace("/", "-").replace(".", "-");
                    userExcel.bornDate = sdf.parse(value);
                }catch (Exception e){

                }
                break;
            case "开始有效期":
                try {
                    value = value.replace("/", "-").replace(".", "-");
                    userExcel.startDate = sdf.parse(value);
                }catch (Exception e){

                }
                break;
            case "截止有效期":
                try {
                    value = value.replace("/", "-").replace(".", "-");
                    userExcel.endDate = sdf.parse(value);
                }catch (Exception e){

                }
                break;
            case "卡号":
                userExcel.cardNum = value;
                break;
            case "身份证号码":
                userExcel.identityNum = value;
                break;
            case "民族":
                Nation nation = nationRepository.findFirstByNameLike(String.format("%%%s%%", value));
                if(nation != null){
                    userExcel.nation = nation;
                }
                break;
            case "家庭住址":
                userExcel.address = value;
                break;
            case "签发机关":
                userExcel.qianfajiguan = value;
                break;
            case "所属岗点":
                Project project = projectRepository.findFirstByNameLike(String.format("%%%s%%", value));
                if(project != null){
                    userExcel.project = project;
                }
                break;
            case "介绍人":
                userExcel.sponsorName = value;
                break;
            case "文件程度":
                Education education = educationRepository.findFirstByNameLike(String.format("%%%s%%", value));
                if(education != null){
                    userExcel.education = education;
                }
                break;
            case "身高":
                try {
                    userExcel.height = Double.parseDouble(value);
                }catch (Exception e){

                }
                break;
            case "政治面貌":
                PoliticalStatus politicalStatus = politicalStatusRepository.findFirstByNameLike(String.format("%%%s%%", value));
                if(politicalStatus != null){
                    userExcel.politicalStatus = politicalStatus;
                }
                break;
            case "兵役":
                MilitaryStatus militaryStatus = militaryStatusRepository.findFirstByNameLike(String.format("%%%s%%", value));
                if(militaryStatus != null){
                    userExcel.militaryStatus = militaryStatus;
                }
                break;
            case "保险情况":
                if("0".equals(value) || "无".equals(value)){
                    userExcel.insurance = 0;
                } else {
                    userExcel.insurance = 1;
                }
                break;
            case "婚姻状况":
                MaritalStatus maritalStatus = maritalStatusRepository.findFirstByNameLike(String.format("%%%s%%", value));
                if(maritalStatus != null){
                    userExcel.maritalStatus = maritalStatus;
                }
                break;
            case "缴纳单位":
                userExcel.jiaonadanwei = value;
                break;
            case "工资":
                try {
                    userExcel.baseWage = Double.parseDouble(value);
                }catch (Exception e){

                }
                break;
            case "入职时间":
                try {
                    value = value.replace("/", "-").replace(".", "-");
                    userExcel.employDate = sdf.parse(value);
                }catch (Exception e){

                }
                break;
        }
    }

    public Department getDepartment() {
        return department;
    }

    public void setDepartment(Department department) {
        this.department = department;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getContact() {
        return contact;
    }

    public void setContact(String contact) {
        this.contact = contact;
    }

    public int getSex() {
        return sex;
    }

    public void setSex(int sex) {
        this.sex = sex;
    }

    public Date getBornDate() {
        return bornDate;
    }

    public void setBornDate(Date bornDate) {
        this.bornDate = bornDate;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    public String getCardNum() {
        return cardNum;
    }

    public void setCardNum(String cardNum) {
        this.cardNum = cardNum;
    }

    public String getIdentityNum() {
        return identityNum;
    }

    public void setIdentityNum(String identityNum) {
        this.identityNum = identityNum;
    }

    public Nation getNation() {
        return nation;
    }

    public void setNation(Nation nation) {
        this.nation = nation;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getQianfajiguan() {
        return qianfajiguan;
    }

    public void setQianfajiguan(String qianfajiguan) {
        this.qianfajiguan = qianfajiguan;
    }

    public Project getProject() {
        return project;
    }

    public void setProject(Project project) {
        this.project = project;
    }

    public String getSponsorName() {
        return sponsorName;
    }

    public void setSponsorName(String sponsorName) {
        this.sponsorName = sponsorName;
    }

    public Education getEducation() {
        return education;
    }

    public void setEducation(Education education) {
        this.education = education;
    }

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }

    public PoliticalStatus getPoliticalStatus() {
        return politicalStatus;
    }

    public void setPoliticalStatus(PoliticalStatus politicalStatus) {
        this.politicalStatus = politicalStatus;
    }

    public MilitaryStatus getMilitaryStatus() {
        return militaryStatus;
    }

    public void setMilitaryStatus(MilitaryStatus militaryStatus) {
        this.militaryStatus = militaryStatus;
    }

    public MaritalStatus getMaritalStatus() {
        return maritalStatus;
    }

    public void setMaritalStatus(MaritalStatus maritalStatus) {
        this.maritalStatus = maritalStatus;
    }

    public int getInsurance() {
        return insurance;
    }

    public void setInsurance(int insurance) {
        this.insurance = insurance;
    }

    public String getJiaonadanwei() {
        return jiaonadanwei;
    }

    public void setJiaonadanwei(String jiaonadanwei) {
        this.jiaonadanwei = jiaonadanwei;
    }

    public Date getEmployDate() {
        return employDate;
    }

    public void setEmployDate(Date employDate) {
        this.employDate = employDate;
    }

    public double getBaseWage() {
        return baseWage;
    }

    public void setBaseWage(double baseWage) {
        this.baseWage = baseWage;
    }

    public SimpleDateFormat getSdf() {
        return sdf;
    }

    public DepartmentRepository getDepartmentRepository() {
        return departmentRepository;
    }

    public void setDepartmentRepository(DepartmentRepository departmentRepository) {
        this.departmentRepository = departmentRepository;
    }

    public NationRepository getNationRepository() {
        return nationRepository;
    }

    public void setNationRepository(NationRepository nationRepository) {
        this.nationRepository = nationRepository;
    }

    public ProjectRepository getProjectRepository() {
        return projectRepository;
    }

    public void setProjectRepository(ProjectRepository projectRepository) {
        this.projectRepository = projectRepository;
    }

    public UserRepository getUserRepository() {
        return userRepository;
    }

    public void setUserRepository(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    public EducationRepository getEducationRepository() {
        return educationRepository;
    }

    public void setEducationRepository(EducationRepository educationRepository) {
        this.educationRepository = educationRepository;
    }

    public PoliticalStatusRepository getPoliticalStatusRepository() {
        return politicalStatusRepository;
    }

    public void setPoliticalStatusRepository(PoliticalStatusRepository politicalStatusRepository) {
        this.politicalStatusRepository = politicalStatusRepository;
    }

    public MilitaryStatusRepository getMilitaryStatusRepository() {
        return militaryStatusRepository;
    }

    public void setMilitaryStatusRepository(MilitaryStatusRepository militaryStatusRepository) {
        this.militaryStatusRepository = militaryStatusRepository;
    }

    public MaritalStatusRepository getMaritalStatusRepository() {
        return maritalStatusRepository;
    }

    public void setMaritalStatusRepository(MaritalStatusRepository maritalStatusRepository) {
        this.maritalStatusRepository = maritalStatusRepository;
    }

    public int getRowNum() {
        return rowNum;
    }

    public void setRowNum(int rowNum) {
        this.rowNum = rowNum;
    }

    public JobNature getJobNature() {
        return jobNature;
    }

    public void setJobNature(JobNature jobNature) {
        this.jobNature = jobNature;
    }
}
